import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shop/model/MyModel.dart';

class ProviderScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Provider(
      create: (_) => MyModel(),
      child: Scaffold(
        appBar: AppBar(
          title: Text("provider"),
        ),
        body: Column(
          children: <Widget>[
            Builder(builder: (context) {
              //获取Provider提供出来的值
              MyModel _model = Provider.of<MyModel>(context);
              return Container(
                margin: const EdgeInsets.only(top: 20),
                width: MediaQuery.of(context).size.width,
                padding: const EdgeInsets.all(20),
                alignment: Alignment.center,
                color: Colors.lightBlueAccent,
                child: Text('当前是：${_model.counter}'),
              );
            }),
            Consumer<MyModel>(
              builder: (context, model, child) {
                return Container(
                  margin: const EdgeInsets.only(top: 20),
                  width: MediaQuery.of(context).size.width,
                  padding: const EdgeInsets.all(20),
                  alignment: Alignment.center,
                  color: Colors.lightGreen,
                  child: Text('${model.counter}'),
                );
              },
            ),
            Consumer<MyModel>(builder: (context, model, child) {
              return Center(
                child: Container(
                  child: Center(
                    child: Column(
                      children: <Widget>[
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: FlatButton(
                            color: Colors.tealAccent,
                            onPressed: model.incrementCounter,
                            child: Icon(Icons.add),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              );
            })
          ],
        ),
      ),
    );
  }
}
